#include	"ScrollTextWarning.hpp"

ScrollTextWarning::ScrollTextWarning(std::list<std::string> listItems, unsigned int heigth, unsigned int width, WINDOW *parentWin)
  :_listItems(listItems), _heightWin(heigth), _widthWin(width), _parentWin(parentWin)
{
  this->_cursor = 0;
  this->_startDisplay = 0;
  this->_nbFlags = 0;
}

ScrollTextWarning::~ScrollTextWarning()
{
  
}

void	ScrollTextWarning::cursorDown()
{
  if (this->_cursor < this->_listItems.size() - 1)
    {
      this->_nbFlags = 0;
      this->_cursor += 1;
    }
  if (this->_cursor - this->_startDisplay >= this->_heightWin - 2)
    this->_startDisplay += 1;
}

void	ScrollTextWarning::cursorUp()
{
  if (this->_startDisplay > 0 && this->_cursor == this->_startDisplay)
    {
      this->_nbFlags = 0;
      this->_startDisplay -= 1;
    }
  if (this->_cursor > 0)
    this->_cursor -= 1;
}

void	ScrollTextWarning::cursorRight()
{
  if (this->_nbFlags < 4)
    this->_nbFlags++;
}

void	ScrollTextWarning::cursorLeft()
{
  if (this->_nbFlags > 0)
    this->_nbFlags--;
}

std::string	ScrollTextWarning::getLineOnCursor()
{
  unsigned int	lines = 0;
  std::list<std::string>::iterator	it;
  
  for (it = this->_listItems.begin(); it != this->_listItems.end(); it++)
    {
      if (lines == this->_cursor)
	return (*it);
      lines++;
    }
}

void	ScrollTextWarning::update()
{
  unsigned int	lines = 0;
  unsigned int	jump = 0;
  unsigned int	stringDeca;
  std::string	strTmp;
  std::list<std::string>::iterator	it;

  for (it = this->_listItems.begin(); it != this->_listItems.end(); it++)
    {
      if (lines >= this->_startDisplay && lines - this->_startDisplay < this->_heightWin - 2)
	{
	  stringDeca = -1;
	  strTmp = (*it);
	  if (strTmp.length() > this->_widthWin - 2)
	    strTmp.resize(this->_widthWin - 2);
	  while (strTmp.length() < this->_widthWin - 2)
	    strTmp += " ";
	  while (++stringDeca < 5)
	    {
	      if (lines == this->_cursor && this->_nbFlags == stringDeca)
		enableColorPair1(this->_parentWin);
	      mvwprintw(this->_parentWin, jump + 1, 1 + stringDeca, "%c", strTmp.at(stringDeca));
	      if (lines == this->_cursor && this->_nbFlags == stringDeca)
		disableColorPair1(this->_parentWin);
	    }
	  jump++;
	}
      lines++;
    }
}

int		ScrollTextWarning::getStartDisplay()const
{
  return (this->_startDisplay);
}
